package com.weixing.oauth2.common.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.TokenRequest;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 解决authorizationServerTokenServices循环依赖问题
 * 暂时没有找到其他解决方法
 *
 * @author paascloud.net@gmail.com
 */
@Component("securitySupportHandler")
@Slf4j
public class SecuritySupportHandler {

	@Resource(name = "defaultAuthorizationServerTokenServices")
	private AuthorizationServerTokenServices authorizationServerTokenServices;


	/**
	 * 刷新token
	 *
	 * @param refreshToken
	 * @param tokenRequest
	 * @return
	 */
	public OAuth2AccessToken refreshAccessToken(String refreshToken, TokenRequest tokenRequest) {
		return authorizationServerTokenServices.refreshAccessToken(refreshToken,tokenRequest);
	}

	/**
	 * 生成access_token
	 * @param oAuth2Authentication
	 * @return
	 */
	public OAuth2AccessToken createAccessToken(OAuth2Authentication oAuth2Authentication){
		return authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
	}
}
